package bjtu.cit.bookshop.controller.admin;

import bjtu.cit.bookshop.common.ErrorCode;
import bjtu.cit.bookshop.common.Md5;
import bjtu.cit.bookshop.controller.BaseController;
import bjtu.cit.bookshop.entities.Employee;
import bjtu.cit.bookshop.entities.Publisher;
import bjtu.cit.bookshop.service.IEmployeeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by jia19 on 2015/10/11.
 */
@RequestMapping("/admin/")
@Controller
public class EmployeeController extends BaseController {
    @Resource
    IEmployeeService employeeService = null;

    //职工管理主页
    @RequestMapping("employee.do")
    public String employee(HttpSession session, ModelMap model, HttpServletRequest request) {
        String loginName = checkAdminLogin(session);
        if (loginName == null) {
            return "/admin/index";
        }

        model.addAttribute("isLogin", true);
        model.addAttribute("loginName", loginName);
        model.addAttribute("current", "employee");
        List<Employee> list = employeeService.getEmployeeList();
        model.addAttribute("employeeList", list);

        return "/admin/employee";
    }

    @RequestMapping("delEmployee.do")
    @ResponseBody
    public ErrorCode delEmployee(HttpServletRequest request, HttpSession session) {
        if (checkAdminLogin(session) == null) {
            return NO_AUTH;
        }
        String employeeId = request.getParameter("id");
        if (employeeId == null) {
            return PARA_ERROR;
        }
        try {
            this.employeeService.deleteById(Integer.valueOf(employeeId));
        } catch (Exception e) {
            e.printStackTrace();
            return PARA_ERROR;
        }
        return SUCCESS;
    }

    @RequestMapping("newEmployee.do")
    public String newEmployee(HttpSession session, HttpServletRequest request, ModelMap model) {
        String loginName = checkAdminLogin(session);
        if (loginName == null) {
            return "/admin/index";
        }
        model.addAttribute("isLogin", true);
        model.addAttribute("loginName", loginName);
        model.addAttribute("current", "employee");

        return "/admin/newEmployee";
    }

    @RequestMapping("addEmployee.do")
    @ResponseBody
    public ErrorCode addEmployee(HttpSession session, HttpServletRequest request) {
        if (checkAdminLogin(session) == null) {
            return NO_AUTH;
        }

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String gender = request.getParameter("gender");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        String role = request.getParameter("role");

        Employee employee = new Employee();
        try {
            employee.setUsername(username);
            employee.setPassword(Md5.getMd5(password));
            employee.setGender(gender);
            employee.setEmail(email);
            employee.setPhone(phone);
            employee.setRole(Integer.valueOf(role));
        } catch (Exception e) {
            e.printStackTrace();
            return PARA_ERROR;
        }
        employeeService.createAEmployee(employee);
        return SUCCESS;
    }


    @RequestMapping("employeeEdit.do")
    public String employeeEdit(HttpSession session, HttpServletRequest request, ModelMap model) {
        String loginName = checkAdminLogin(session);
        if (loginName == null) {
            return "/admin/index";
        }
        model.addAttribute("isLogin", true);
        model.addAttribute("loginName", loginName);
        model.addAttribute("current", "employee");

        String id = request.getParameter("id");
        if (id == null) {
            return "admin/badParameter";
        }
        try {
            Employee employee = employeeService.getEmployeeById(Integer.valueOf(id));
            model.addAttribute("employee", employee);
        } catch (Exception e) {
            e.printStackTrace();
            return "admin/badParameter";
        }

        return "admin/employeeEdit";

    }

    @RequestMapping("employeeSave.do")
    @ResponseBody
    public ErrorCode employeeSave(HttpSession session, HttpServletRequest request) {
        if (checkAdminLogin(session) == null) {
            return NO_AUTH;
        }

        String id = request.getParameter("employeeId");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String gender = request.getParameter("gender");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        String role = request.getParameter("role");

        Employee employee = new Employee();
        try {
            employee.setId(Integer.valueOf(id));
            employee.setUsername(username);
            employee.setPassword(Md5.getMd5(password));
            employee.setGender(gender);
            employee.setEmail(email);
            employee.setPhone(phone);
            employee.setRole(Integer.valueOf(role));
        } catch (Exception e) {
            e.printStackTrace();
            return PARA_ERROR;
        }
        employeeService.updateEmployeeInfo(employee);
        return SUCCESS;

    }

}
